package leetcode算法面试题;

public class 合并两个有序数组 {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
          int i = 0;
          int j = 0;
            int begin =0;
        int[] res = new int[m + n];
        while (i<m&&j<n){
              if (nums1[i]<=nums2[j]){
                 res[begin++] = nums1[i];
                 i++;
              }else {
                 res[begin++] = nums2[j];
                 j++;
              }
          }
          //剩下的都加上去
        for (; i < m; ) {
            res[begin++] = nums1[i++];
        }
        for (; j < n; ) {
            res[begin++] = nums2[j++];
         }
        for (int k = 0; k < m + n; k++) {
            nums1[k] = res[k];
        }
    }
}
